import XEUtils from "xe-utils";
export default {
    tabledataup(obj, currRowIndex, _this) {
        if (currRowIndex > 0) {
            let upData = obj.data[currRowIndex - 1];
            let currentrow = obj.data[currRowIndex];
            obj.data.splice(currRowIndex - 1, 1);
            obj.data.splice(currRowIndex, 0, upData);
            currRowIndex = currRowIndex - 1;
            _this.$nextTick(() => {
                obj.setCurrentRow(currentrow);
            });
        }
        return {
            currRowIndex: currRowIndex,
        };
    },
    tabledatadown(obj, currRowIndex, _this) {
        if (currRowIndex !== obj.data.length - 1) {
            let downData = obj.data[currRowIndex + 1];
            let currentrow = obj.data[currRowIndex];
            obj.data.splice(currRowIndex + 1, 1);
            obj.data.splice(currRowIndex, 0, downData);
            currRowIndex = currRowIndex + 1;
            _this.$nextTick(() => {
                obj.setCurrentRow(currentrow);
            });
        }
        return {
            currRowIndex: currRowIndex,
        };
    },
    tabledatalast(obj, currRowIndex, _this) {
        if (currRowIndex != obj.data.length - 1) {
            let lastData = obj.data[currRowIndex];
            obj.data.splice(currRowIndex, 1);
            obj.data.splice(obj.data.length, 0, lastData);
            currRowIndex = obj.data.length - 1;
            _this.$nextTick(() => {
                obj.setCurrentRow(lastData);
            });
        }
        return {
            currRowIndex: currRowIndex,
        };
    },
    tabledatafirst(obj, currRowIndex, _this) {
        if (currRowIndex > 0) {
            let firstData = obj.data[currRowIndex];
            obj.data.splice(currRowIndex, 1);
            obj.data.splice(0, 0, firstData);
            currRowIndex = 0;
            _this.$nextTick(() => {
                obj.setCurrentRow(firstData);
            });
        }
        return {
            currRowIndex: currRowIndex,
        };
    },
    //vxe-table删除一条后自动选中上一行
    DelRecordSelect(table, index, that) {
        var delrow = table.getCurrentRow();
        var delindex = index;
        if (delindex < 0) {
            delindex = table.getRowIndex(delrow);
        }
        table.remove(delrow);
        if (delindex > -1 && table.tableData.length > 0) {
            if (table.getTableData().fullData.length > delindex) {
                delindex = delindex;
            } else {
                delindex = delindex - 1;
            }
            var selectrow = table.getTableData().fullData[delindex];
            table.setCurrentRow(selectrow);
            return selectrow;
        } else {
            var nowdata = table.getTableData().fullData.filter(item => {
                return item._XID < delrow._XID;
            });
            if (nowdata.length > 0) {
                var selectedrow = nowdata[nowdata.length - 1];
                table.setCurrentRow(selectedrow);
                return selectedrow;
            }
        }
    },
    //vxe-table新增行自动选中，table为vxetable，row新增行，column新增列激活单元格
    AddRecordSelect(table, row, column) {
        if (table && row) {
            table.insertAt(row, -1).then(() => {
                var curow = table.getTableData().fullData[table.getTableData().fullData.length - 1];
                table.setCurrentRow(curow);
                if (column) {
                    table.setActiveCell(curow, column);
                }
            });
        }
    },
    //自动加载表格的第一行，并执行回调函数
    loadFirstRow(xTable, func) {
        this.$nextTick(() => {
            // 加载第一行
            var selectrow = xTable.getData(0);
            if (selectrow) {
                xTable.setCurrentRow(selectrow).then(() => {
                    xTable.scrollToRow(selectrow).then(() => {
                        if (typeof func == "function") func(selectrow); //滚动到行后操作，如用来高亮后赋值
                    }); //滚动到行
                }); //高亮行
            } else {
                xTable.setCurrentRow(null); //这个不会自动清空
            }
        });
    }
};
